package logic

import (
	"context"
	"fmt"
	"time"
	"weekly/model/cache"

	"github.com/mojocn/base64Captcha"
	"weekly/weeklyrpc/internal/svc"
	"weekly/weeklyrpc/weeklyrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CaptchaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CaptchaLogic {
	return &CaptchaLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CaptchaLogic) Captcha(in *weeklyrpc.CaptchaRequest) (*weeklyrpc.CaptchaResponse, error) {
	// todo: add your logic here and delete this line

	c := base64Captcha.NewCaptcha(base64Captcha.DefaultDriverDigit, base64Captcha.DefaultMemStore)
	generate, s, s2, err := c.Generate()
	if err != nil {
		return nil, err
	}
	fmt.Println(generate, s, s2)

	cache.Rdb.Set(l.ctx, "CaptchaKey"+in.Account, s2, time.Minute*5)
	return &weeklyrpc.CaptchaResponse{
		CaptchaImage: s,
	}, nil
}
